home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Memory / Pools / FinitePoolBase.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  1.1 KB  |  52 lines  |  [TEXT/CWIE]

  1. // FinitePoolBase.cp
  2.  
  3. #ifndef FinitePoolBase_h
  4. #include "FinitePoolBase.h"
  5. #endif
  6.  
  7. FinitePoolBase::FinitePoolBase( void *theSpace,
  8.                                           BitArrayBase& theMap,
  9.                                           uint32 theElementSize )
  10.   : space( static_cast<uint8 *>(theSpace) ),
  11.      map( theMap ),
  12.      elementSize( theElementSize ),
  13.      allocated( 0 ),
  14.      nextAllocation( 0 )
  15.   {
  16.     Assert( CanMultiply( theMap.Length(), theElementSize ) )
  17.     map.ClearAll();
  18.   }
  19.  
  20. void *FinitePoolBase::Allocate( uint32 size )
  21.   {
  22.     Assert( size == elementSize );
  23.     Assert( !Full() );
  24.     
  25.     uint32 slot = map.FirstZero( nextAllocation );
  26.     if ( slot >= map.Length() )
  27.         slot = map.FirstZero();
  28.     
  29.     Assert( slot < map.Length() );
  30.     nextAllocation = slot + 1;
  31.     
  32.     map[ slot ].Set();
  33.     allocated++;
  34.     return space + size * slot;
  35.   }
  36.  
  37. void FinitePoolBase::Release( void *vp )
  38.   {
  39.     uint8 *const p = static_cast< uint8 * >( vp );
  40.     
  41.     Assert( p >= space );
  42.     Assert( p - space < map.Length() * elementSize );
  43.     Assert( (p - space) % elementSize == 0 );
  44.     Assert( !IsEmpty() );
  45.     
  46.     uint32 slot = (p - space) / elementSize;
  47.     Assert( map[ slot ] );
  48.     
  49.     map[ slot ].Clear();
  50.     allocated--;
  51.   }
  52.